package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.businesslogic.BrandDeleteProcess;
import com.fan.shop.domain.Brand;
import com.fan.shop.global.JacksonMapper;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.services.BrandService;

@ManagedBean(name = "brandsBean")
@RequestScoped
public class BrandsBean extends ShopBean {

	private static Logger logger = Logger.getLogger(BrandsBean.class);

	@ManagedProperty(value = "#{brandService}")
	private BrandService brandService;

	private String tableData;
	private int columnsSize;
	private List<String> columnNames;

	private String name;
	private Brand selectedBrand;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		BrandsBean.logger = logger;
	}

	public BrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : brandService.getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : brandService.getList()) {
			try {
				Object brandUserClass = brandService.copy(obj);
				String line = mapper.writer(filters).writeValueAsString(
						brandUserClass);
				data = data.append(line);

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";
		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public int getColumnsSize() {
		columnsSize = getColumnNames().size();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = brandService.getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Brand getSelectedBrand() {
		if(selectedBrand == null)
		{
			if(name != null && name.trim().length() > 0)
			{
				selectedBrand = brandService.getBrand(name);
			}
		}
		return selectedBrand;
	}

	public void setSelectedBrand(Brand selectedBrand) {
		this.selectedBrand = selectedBrand;
	}

	
	public String openBrandAction()
	{
		return "edit_brand.faces";
	}
	
	public String createBrandAction()
	{
		return "edit_brand.faces";
	}

	public String deleteBrandAction()
	{
		BrandDeleteProcess brandDeleteProcess = new BrandDeleteProcess();
		brandDeleteProcess.setBrand(getSelectedBrand());
		brandDeleteProcess.setBrandService(brandService);
		int result = brandDeleteProcess.process();
		if(result == ShopGlobalVariables.FAILED)
		{
			logger.debug(brandDeleteProcess.getErrorMessage());
		}
		return "brands.faces";
	}
}
